
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<span style="font-size:18px;">
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="approvalProcess/css/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="approvalProcess/css/commonly-used-show.css" />
    <script type="text/javascript" src="approvalProcess/js/jquery.min.js"></script>
    <script type="text/javascript" src="approvalProcess/js/bootstrap.js" ></script>
    <script type="text/javascript" src="approvalProcess/js/jquery.cookie.js"></script>

    <title>添加用户</title>

</head>
<body class="mybody">

<jsp:include page="theTopMenuAdmin.jsp" />


<div class="container-fluid">
    <div class="row">


        <div class="col-md-12">
            <div class="col-md-12">
                    <div class="panel panel-primary">
                        <div class="panel-body">
                            <div class="alert alert-success">



                                <div class="container">
        <h3 style="text-align: center">添加用户信息</h3>
        <hr style="filter: alpha(opacity=100,finishopacity=0,style=3)" width="100%" color="#6f5999" size="3" />

        <div id="add_user" class="container">
            <div class="form-group same-line">
                <label ><b style="color: red">*&nbsp</b>登录名或账号：</label>
                <input type="text" id="user_loing_name" class="form-control edit" placeholder="请输入登录名或账号" >
            </div>

            <div class="form-group same-line-left">
                <label ><b style="color: red">*&nbsp</b>登录密码：</label>
                <input type="text" id="user_loing_passwd" class="form-control edit" placeholder="请输入登录密码" >
            </div>

            <div class="form-group same-line-left">
                <label ><b style="color: red">*&nbsp</b>用户类型：</label>
                <select class="form-control edit" title="请选择" id="user_level" data-live-search="true" data-size="5">
                    <option value="" disabled selected hidden>请选择</option>
                    <option value="0">管理员</option>
                    <option value="1">教师</option>
                    <option value="2">学生</option>
                    <option value="3">院领导</option>
                    <option value="4">校领导</option>
                    <option value="5">门卫</option>
                </select>
            </div>

            <div class="form-group same-line">
                <label ><b style="color: red">*&nbsp</b>用户名：</label>
                <input type="text" id="user_name" class="form-control edit" placeholder="请输入用户名" >
            </div>

            <div class="form-group same-line-left">
                <label><b style="color: red">*&nbsp</b>用户性别：</label>
                <select class="form-control edit" title="请选择" id="user_sex" data-live-search="true" data-size="5">
                    <option value="" disabled selected hidden>请选择</option>
                    <option value="男">男</option>
                    <option value="女">女</option>
                </select>
            </div>

            <div class="form-group same-line-left">
                <label><b style="color: red">*&nbsp</b>学院：</label>
                <select style="font-size:14px" class="form-control edit" title="请选择" id="name_college" data-live-search="true">
                    <option value="" disabled selected hidden>请选择</option>
                    <option value="继续教育学院">继续教育学院</option>
                    <option value="国际学院">国际学院</option>
                    <option value="大健康学院">大健康学院</option>
                     <option value="材料与化学工程学院">材料与化学工程学院</option>
                     <option value="大数据学院">大数据学院</option>
                    <option value="农林工程与规划学院">农林工程与规划学院</option>
                    <option value="经济管理学院">经济管理学院</option>
                    <option value="艺术学院">艺术学院</option>
                    <option value="人文学院">人文学院</option>
                    <option value="教育学院">教育学院</option>
                    <option value="马克思主义学院">马克思主义学院</option>
                    <option value="乌江学院">乌江学院</option>
                     <option value="国学院">国学院</option>
                </select>
            </div>

            <div class="form-group same-line">
                    <label ><b style="color: red">*&nbsp</b>职务：</label>
                    <select class="form-control edit" title="请选择" id="instructor_name" data-live-search="true" >
                    <option value="" disabled selected hidden>请选择</option>
                    <option value="管理员">管理员</option>
                    <option value="学生">学生</option>
                    <option value="教师">教师</option>
                    <option value="班主任">班主任</option>
                    <option value="院领导">院领导</option>
                    <option value="校领导">校领导</option>
                    <option value="门卫">门卫</option>
                </select>
                </div>

            <div class="form-group same-line-left">
                <label >生日：</label>
                <input type="date" id="user_brithday" class="form-control edit" placeholder="请输入用户生日" >
            </div>

            <div class="form-group same-line-left">
                <label >用户地址：</label>
                <input type="text" id="user_address" class="form-control edit" placeholder="请输入地址" >
            </div>

            <div class="form-group same-line">
                <label >用户座机：</label>
                <input type="text" id="user_phone" class="form-control edit" placeholder="请输入用户座机" >
            </div>

            <div class="form-group same-line-left">
                <label >用户手机号：</label>
                <input type="text" id="user_tel" class="form-control edit" placeholder="请输入用户手机号" >
            </div>

            <div class="form-group same-line-left">
                <label >用户邮箱：</label>
                <input type="email" id="user_email" class="form-control edit" placeholder="请输入用户邮箱" >
            </div>

        </div>

        <div style="margin-top: 50px;" class="form-group text-center same-line-clear">
            <input id="update_user" type="button" value="添加" class="btn btn-primary" >
            <input type="reset" value="重置" class="btn btn-danger interval" onclick="reset()">
        </div>

        <div class="text-center">
            <label id="user_null" style="color: red;"></label>
        </div>

</div>



                            </div>
                        </div>
                    </div>
                </div>
        </div>



    </div>

 </div>




<script type="text/javascript">
    var userdt = JSON.parse(sessionStorage.getItem("user"));
    var userNull = document.getElementById("user_null");
    if (userdt.result.userName != null){
        $("#usname").text(userdt.result.userName);
    }else {
        $("#usname").text(userdt.result.userLoginName);
    }

    function reset(){
        var addUser = document.getElementById("add_user");
        var ipts = addUser.getElementsByTagName("input");
        for (var i = 0; i < ipts.length; i++){
            ipts[i].value = "";
        }
    }

    window.onload = function(){
        if ("1" !== "${param.id}"){
            userInfo();

            $("#update_user").attr("value","确认修改");
            $("#update_user").click(function(){
                updateUserInfo();
            });
        }else {
            $("#update_user").click(function(){
                addUser();
            });
        }
    }

    function updateUserInfo(){
        var postUrl = "${pageContext.request.contextPath}/updateUser";

        var str = {
            "id": "${param.id}",
            "userLoginName" : $("#user_loing_name").val(),
            "userLoginPwd" : $("#user_loing_passwd").val(),
            "userLevel" : $("#user_level").val(),
            "userReplenish" : {
                "id": "${param.id}",
                "teacherCharge" : $("#instructor_name").val(),
                "belongsCollege" : $("#name_college").val()
            },
            "userName" : $("#user_name").val(),
            "userSex" : $("#user_sex").val(),
            "userBrithday" : $("#user_brithday").val(),
            "userAddress" : $("#user_address").val(),
            "userTel" : $("#user_tel").val(),
            "userEmail" : $("#user_email").val()
        };

        $.ajax({
            type:"post",
            url:postUrl,
            headers: {
                "Content-Type":"application/json"
            },
            data:JSON.stringify(str),
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            success:function(data){
                if (data.status == "0"){
                    userNull.innerHTML = data.msg;
                }else {
                    location.reload();
                }

            },
            error:function(xhr,state,errorThrown){
                //alert("发生网络错误，错误码为：" + xhr.status)
                location.href = "${pageContext.request.contextPath}/error";
            }
        });
    }

    function userInfo(){
        var postUrl = "${pageContext.request.contextPath}/userInfo";

        var str = {
            "id": "${param.id}"
        };

        $.ajax({
            type:"post",
            url:postUrl,
            headers: {
                "Content-Type":"application/json"
            },
            data:JSON.stringify(str),
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            success:function(data){
                if (data.status == "0"){
                    userNull.innerHTML = data.msg;
                }else {
                    $("#user_loing_name").val(data.result.userLoginName);
                    $("#user_loing_passwd").val(data.result.plaintPwd);
                    $("#user_level").val(data.result.userLevel);
                    $("#instructor_name").val(data.result.userReplenish.teacherCharge);
                    $("#user_name").val(data.result.userName);
                    $("#user_sex").val(data.result.userSex);
                    $("#name_college").val(data.result.userReplenish.belongsCollege);
                    $("#user_brithday").val(data.result.userBrithday);
                    $("#user_address").val(data.result.userAddress);
                    $("#user_phone").val(data.result.userPhone);
                    $("#user_tel").val(data.result.userTel);
                    $("#user_email").val(data.result.userEmail);

                }

            },
            error:function(xhr,state,errorThrown){
                //alert("发生网络错误，错误码为：" + xhr.status)
                location.href = "${pageContext.request.contextPath}/error";
            }
        });

    }

    function addUser(){
        var userLoingName = $("#user_loing_name").val();
        var userLoingPasswd = $("#user_loing_passwd").val();
        var userLevel = $("#user_level").val();

        var userName = $("#user_name").val();
        var userSex = $("#user_sex").val();
        var userEmail = $("#user_email").val();
        var userBrithday = $("#user_brithday").val();
        var userAddress = $("#user_address").val();
        var userPhone = $("#user_phone").val();
        var userTel = $("#user_tel").val();

        var mInstructorName = $("#instructor_name").val();
        var mNameCollege = $("#name_college").val();

        if ($.isEmptyObject(userLoingName)){
            userNull.innerHTML = "登录名或账号不能为空";
            return false;
        }else if ($.isEmptyObject(userLoingPasswd)){
            userNull.innerHTML = "密码不能为空";
            return false;
        }else if ($.isEmptyObject(userLevel)){
            userNull.innerHTML = "用户类型不能为空";
            return false;
        }else if ($.isEmptyObject(userName)){
            userNull.innerHTML = "用户姓名不能为空";
            return false;
        }else if ($.isEmptyObject(userSex)){
            userNull.innerHTML = "用户性别不能为空";
            return false;
        }else if ($.isEmptyObject(mNameCollege)){
            userNull.innerHTML = "学院不能为空";
            return false;
        }else if ($.isEmptyObject(mInstructorName)){
            userNull.innerHTML = "辅导员不能为空";
            return false;
        }else {
            userNull.innerHTML = "";
        }


        var postUrl = "${pageContext.request.contextPath}/insertUser";
        var str = {
            "userLoginName": userLoingName,
            "userLoginPwd": userLoingPasswd,
            "userLevel": userLevel,
            "userName": userName,
            "userSex": userSex,
            "userEmail": userEmail,
            "userBrithday": userBrithday,
            "userAddress": userAddress,
            "userPhone": userPhone,
            "userTel": userTel,
            "userReplenish": {
                "belongsCollege": mNameCollege,
                "teacherCharge": mInstructorName
            }
        };

        $.ajax({
            type:"post",
            url:postUrl,
            headers: {
                "Content-Type":"application/json"
            },
            data:JSON.stringify(str),
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            success:function(data){
                if (data.status == "0"){
                    userNull.innerHTML = data.msg;
                }else {
                    location.href = "${pageContext.request.contextPath}/adminStrationManagement?level=3";
                }

            },
            error:function(xhr,state,errorThrown){
                //alert("发生网络错误，错误码为：" + xhr.status)
                location.href = "${pageContext.request.contextPath}/error";
            }
        });
    }

</script>

</body>
</html>
</span>